﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using SQLWrapper;
using Model.App;
using Model.Images;
using Model.Weixin;
using Services.Common;
using Services.Group;

namespace iShangMai.Controllers.App
{
    public class GroupAccessController : ApiController
    {
        [HttpGet]
        public async Task<ZKResponse> GetGroupJoins(UInt64 uid, UInt64 group_id, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetGroupJoinRequest request = new ISMGetGroupJoinRequest()
            {
                uid = uid,
                group_id = group_id,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_GROUP_JOINS,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetGroupMembers(UInt64 uid, UInt64 group_id,string nostr, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetGroupMembersRequest request = new ISMGetGroupMembersRequest()
            {
                uid = uid,
                group_id = group_id,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_GROUP_MEMBERS,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> ExitGroup(UInt64 exitUid, UInt64 group_id, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMRemoveUserFromGroupRequest request = new ISMRemoveUserFromGroupRequest()
            {
                group_id = group_id,
                group_operator = exitUid,
                uid = exitUid,
                token = token
            };
            ISMServiceModel model = new ISMServiceModel(){
                operationIdentifier = ISMGroupServiceProvider.EXIT_GROUP,
                userinfo = request
            };
            ISMGroupServiceProvider provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> DismissGroup(UInt64 operatorId,UInt64 group_id,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMDismissGroupRequest request = new ISMDismissGroupRequest()
            {
                group_id = group_id,
                uid = operatorId,
                token = token
            };
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.DISMISS_GROUP,
                userinfo = request
            };
            ISMGroupServiceProvider provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
    }
}
